package com.zjeav.lingjiao.base.tools;


import android.os.Build;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.config.BaseConfig;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by 5zhi on 2016/9/28.
 */
public class RetrofitInstance {
    private static Retrofit jsonInstance = null;//JSon格式的Retrifit
    private static Retrofit jsonTokenInstance=null;
    private static Retrofit formInstance = null;//表单 调用微信时使用
    private static Retrofit WXIntance=null;


    public static void reSetRetrofit() {
        jsonInstance = null;
        formInstance = null;
    }


    public static Retrofit getJsonInstance() {
        if (jsonInstance == null) {
            Interceptor headerInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    boolean b=(boolean)SharedPreferencesUtils.getParam(BaseApplication.getContext(),"isLogin",false);
                    Request.Builder builder=chain.request().newBuilder();
                    if (b){
                        builder.addHeader("authtoken",SharedPreferencesUtils.getParam(BaseApplication.getContext(),"authtoken","").toString());
                    }
                    Request original = builder
                            .addHeader("Content-Type", "application/json")
                            .method(chain.request().method(), chain.request().body())
                            .build();

                    return chain.proceed(original);
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(headerInterceptor)
                    .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                    .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                    .sslSocketFactory(BaseApplication.sslParams.sSLSocketFactory, BaseApplication.sslParams.trustManager)
//                    .hostnameVerifier(new HostnameVerifier() {
//                        @Override
//                        public boolean verify(String s, SSLSession sslSession) {
//                            return true;
//                        }
//                    })
                    .build();
            jsonInstance = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BaseConfig.API_SERVICE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return jsonInstance;
        }
        return jsonInstance;
    }
    public static Retrofit getJsonTokenInstance() {
        if (jsonTokenInstance == null) {
            Interceptor headerInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request().newBuilder()
                            .addHeader("authtoken",SharedPreferencesUtils.getParam(BaseApplication.getContext(),"authtoken","").toString())
                            .addHeader("Content-Type", "application/json")
                            .method(chain.request().method(), chain.request().body())
                            .build();

                    return chain.proceed(original);
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(headerInterceptor)
                    .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                    .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                    .sslSocketFactory(BaseApplication.sslParams.sSLSocketFactory, BaseApplication.sslParams.trustManager)
//                    .hostnameVerifier(new HostnameVerifier() {
//                        @Override
//                        public boolean verify(String s, SSLSession sslSession) {
//                            return true;
//                        }
//                    })
                    .build();
            jsonInstance = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BaseConfig.API_SERVICE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return jsonInstance;
        }
        return jsonInstance;
    }

    public static Retrofit getFormInstance() {
        if (formInstance == null) {
            Interceptor headerInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request().newBuilder()
//                            .addHeader("Accept", "*/*")
                            .addHeader("Accept", "application/json")
                            .method(chain.request().method(), chain.request().body())
                            .build();
                    return chain.proceed(original);
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(headerInterceptor)
                    .connectTimeout(1000L, TimeUnit.MILLISECONDS)
                    .readTimeout(1000L, TimeUnit.MILLISECONDS)
//                    .sslSocketFactory(BaseApplication.sslParams.sSLSocketFactory, BaseApplication.sslParams.trustManager)
                    .hostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String s, SSLSession sslSession) {
                            return true;
                        }
                    })
                    .build();
            formInstance = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BaseConfig.API_SERVICE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return formInstance;

        }
        return formInstance;
    }
    public static Retrofit getAccessIntance() {
        if (WXIntance == null) {
            Interceptor headerInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request().newBuilder()
                            .addHeader("content-type", "application/x-www-form-urlencoded")
                            .method(chain.request().method(), chain.request().body())
                            .build();
                    return chain.proceed(original);
                }
            };
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(headerInterceptor)
                    .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                    .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                    .sslSocketFactory(BaseApplication.sslParams.sSLSocketFactory, BaseApplication.sslParams.trustManager)
//                    .hostnameVerifier(new HostnameVerifier() {
//                        @Override
//                        public boolean verify(String s, SSLSession sslSession) {
//                            return true;
//                        }
//                    })
                    .build();
            WXIntance = new Retrofit.Builder()
                    .baseUrl(BaseConfig.WX_SERVICE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return WXIntance;
        }
        return WXIntance;
    }




}

